/*	Author		: Arga, Fan
 *	Created On	: Apr 28, 2011
 *	Description	: 
 */

package team8.paacdroid.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import team8.paacdroid.R;
import team8.paacdroid.bean.Itinerary;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.RelativeLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class ViewMapActivity extends MapActivity {
	private MapController mapController;
	private MapView mapView;
	private LocationManager locationManager;
	private double currentLocLong, toLong, fromLong;
	private double currentLocLat, toLat, fromLat;
	private String toStopName, fromStopName;

	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.view_map_activity); // bind the layout to the activity

		// Create a map view
		RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapView.setStreetView(true);
		mapController = mapView.getController();
		mapController.setZoom(14); // Zoom 1 is world view
		
		// Get the location providers and current location
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, new GeoUpdateHandler());
		Criteria criteria = new Criteria();
		String provider = locationManager.getBestProvider(criteria, false);
		Location location = locationManager.getLastKnownLocation(provider);
		
		// Animate the map to go to current location
		this.currentLocLat = location.getLatitude();
		this.currentLocLong = location.getLongitude();
		int lat = (int) (currentLocLat * 1E6);
		int lng = (int) (currentLocLong * 1E6);
		GeoPoint point = new GeoPoint(lat, lng);
		mapController.animateTo(point); //	mapController.setCenter(point);
		
		// Prepare the overlay for current location
		Drawable currentLocMarker = getResources().getDrawable(R.drawable.mylocation);
		currentLocMarker.setBounds(0, 0, currentLocMarker.getIntrinsicWidth(), currentLocMarker.getIntrinsicHeight());
		mapView.getOverlays().add(new CurrentLocation(currentLocMarker));
		
		// Load the data from bundle
		HashMap<String, String> geoLocPackage = (HashMap<String, String>) getIntent().getSerializableExtra("Route");
		if (geoLocPackage != null) { 
			toLong = geoLocPackage.get("toLong") != null ? Double.parseDouble(geoLocPackage.get("toLong")) : 0d;
			fromLong = geoLocPackage.get("fromLong") != null ? Double.parseDouble(geoLocPackage.get("fromLong")) : 0d;
			toLat = geoLocPackage.get("toLat") != null ? Double.parseDouble(geoLocPackage.get("toLat")) : 0d;
			fromLat = geoLocPackage.get("fromLat") != null ? Double.parseDouble(geoLocPackage.get("fromLat")) : 0d;
			toStopName = geoLocPackage.get("toStopName") != null ? geoLocPackage.get("toStopName") : "";
			fromStopName = geoLocPackage.get("fromStopName") != null ? geoLocPackage.get("fromStopName") : "";
			
			// Prepare the overlay from the to and from location
			Drawable toMarker = getResources().getDrawable(R.drawable.pin_off);
			Drawable fromMarker = getResources().getDrawable(R.drawable.pin_board);
			toMarker.setBounds(0, 0, toMarker.getIntrinsicWidth(), toMarker.getIntrinsicHeight());
			fromMarker.setBounds(0, 0, fromMarker.getIntrinsicWidth(), fromMarker.getIntrinsicHeight());
			mapView.getOverlays().add(new ToLocation(toMarker));
			mapView.getOverlays().add(new FromLocation(fromMarker));
		}
	}
	
	class FromLocation extends ItemizedOverlay {
		private List<OverlayItem> locations = new ArrayList<OverlayItem>();
		private Drawable marker;
		
		public FromLocation(Drawable marker) {
			super(marker);
			this.marker = marker;

			GeoPoint fromLoc = new GeoPoint((int) (fromLat * 1E6), (int) (fromLong * 1E6));

			locations.add(new OverlayItem(fromLoc, fromStopName, fromStopName));

			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);

			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return locations.get(i);
		}

		@Override
		public int size() {
			return locations.size();
		}
	}
	
	class ToLocation extends ItemizedOverlay {
		private List<OverlayItem> locations = new ArrayList<OverlayItem>();
		private Drawable marker;
		
		public ToLocation(Drawable marker) {
			super(marker);
			this.marker = marker;

			GeoPoint toLoc = new GeoPoint((int) (toLat * 1E6), (int) (toLong * 1E6));

			locations.add(new OverlayItem(toLoc, toStopName, toStopName));

			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);

			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return locations.get(i);
		}

		@Override
		public int size() {
			return locations.size();
		}
	}

	class CurrentLocation extends ItemizedOverlay {
		private List<OverlayItem> locations = new ArrayList<OverlayItem>();
		private Drawable marker;

		public CurrentLocation(Drawable marker) {
			super(marker);
			this.marker = marker;

			GeoPoint curLoc = new GeoPoint((int) (currentLocLat * 1E6), (int) (currentLocLong * 1E6));

			locations.add(new OverlayItem(curLoc, "You're here", "You're here"));

			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);

			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return locations.get(i);
		}

		@Override
		public int size() {
			return locations.size();
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return true;
	}

	public class GeoUpdateHandler implements LocationListener {
		@Override
		public void onLocationChanged(Location location) {
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);
			GeoPoint point = new GeoPoint(lat, lng);
			mapController.animateTo(point); //	mapController.setCenter(point);
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}
}
